public class MyLinkedList {
        static class ListNode{
            public ListNode prev;
            public ListNode next;
            public int val;
            public ListNode(int val) {
                this.val = val;
            }
        }
        public ListNode head;
        public ListNode last;
        public void creatList(){
            ListNode node1 = new ListNode(12);
            ListNode node2 = new ListNode(23);
            ListNode node3 = new ListNode(34);
            ListNode node4 = new ListNode(45);
            ListNode node5 = new ListNode(56);
            node1.next = node2;
            node2.next = node3;
            node3.next = node4;
            node4.next = node5;
            this.head = node1;
            this.last = node5;
        }
        public int size(){
            ListNode cur = head;
            int count = 0;
            while (cur != null){
                count++;
                cur = cur.next;
            }
            return count;
        }
        public void display(){
            ListNode cur = head;
            while (cur != null){
                System.out.print(cur.val +  " ");
                cur = cur.next;
            }
            System.out.println();
        }

        public boolean contains(int index){
            ListNode cur = head;
            while (cur != null){
                if (cur.val == index){
                    return  true;
                }
                cur = cur.next;
            }
            return false;
        }

        public void addFirst(int val){
            ListNode next;
            ListNode prev;
            ListNode node = new ListNode(val);
            if (head != null){
                node.next = head;
                head.prev = node;
                head = node;
            }else {
                head = node;
                last = node;
            }
        }

        public void addLast(int val){
            ListNode node = new ListNode(val);
            if (head == null){
                head = node;
                last = node;
            }else {
                last.next = node;
                node.prev = last;
                last = last.next;
            }
        }
        public ListNode search(int index){
            ListNode cur = head;
            while (index != 0){
                cur = cur.next;
                index--;
            }
            return cur;
        }
        public void addIndex(int data,int index){
            if (index < 0 || index > size()){
                //throw new no("位置不合法");
            }
            if (index == 0){
                addFirst(data);
            }
            if (index == size()){
                addLast(data);
            }
            ListNode cur = search(index);
            ListNode node = new ListNode(data);
            node.next = cur;
            cur.prev .next = node;
            node.prev = cur.prev;
            cur.prev = node;
        }

        public void remove(int key){
            ListNode cur = head;
            while (cur != null){
                if (cur.val == key){
                    if (cur == head){
                        //删除头节点
                        head = head.next;
                        if (head != null){
                            //考虑只有一个节点的情况下
                            head.prev = null;
                        }else {
                            last =null;
                        }
                    }else {
                        //删除中间节点和尾巴节点
                        if (cur.next != null){
                            cur.prev.next = cur.next;
                            cur.next.prev = cur.prev;
                        }else {
                            cur.prev.next = cur.next;
                            last = last.prev;
                        }
                    }
                    return;
                }else {
                    cur = cur.next;
                }
            }
        }
        public void removeAll(int key){
            ListNode cur = head;
            while (cur != null){
                if (cur.val == key){
                    if (cur == head){
                        //删除头节点
                        head = head.next;
                        if (head != null){
                            //考虑只有一个节点的情况下
                            head.prev = null;
                        }else {
                            last =null;
                        }
                    }else {
                        //删除中间节点和尾巴节点
                        if (cur.next != null){
                            cur.prev.next = cur.next;
                            cur.next.prev = cur.prev;
                        }else {
                            cur.prev.next = cur.next;
                            last = last.prev;
                        }
                    }
                    cur = cur.next;
                }else {
                    cur = cur.next;
                }
            }
        }
        public void clear(){
            ListNode cur = head;
            while (cur != null){
                ListNode curNext = cur.next;
                cur.prev = null;
                cur.next = null;
                cur = curNext;
            }
            head = null;
            last = null;
        }
    }
